#!/bin/env bash
black="\e[30m"
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
purple="\033[35m"
cyan="\033[36m"
white="\033[37m"
background="\033[0m"

if ! [ "$(uname)" == "Linux" ];then
    echo -e ${red}请使用linux!${background}
    exit
fi

if [ "$(id -u)" != "0" ]; then
    echo -e ${red} 请使用root用户!${background}
    exit
fi

function Dependency(){
apt_update(){
if [ -z ${apt_update} ]
then
  echo -e ${green}正在更新 ${yellow}软件源${background}
  if ! apt update -y
  then
    echo -e ${red}更新出错${background}
    exit 1
  fi
  apt_update=apt_updated
fi
}
pacman_Syyu(){
if [ -z ${pacman_Syyu} ]
then
  echo -e ${green}正在 ${yellow}更新${background}
  if ! pacman -Syyu --noconfirm --needed
  then
    echo -e ${red}更新出错${background}
    exit 1
  fi
  pacman_Syyu=pacman_Syyued
fi
}
InstallDependency(){
echo -e ${green}正在安装必要依赖 $1${background}
if [ $(command -v apt) ];then
  apt_update
  apt install -y $1
elif [ $(command -v pacman) ];then
  pacman_Syyu
  pacman -S --noconfirm --needed dialog
fi
}
if [ -x "$(command -v whiptail)" ];then
  TUISoftware=whiptail
elif [ -x "$(command -v dialog)" ];then
  TUISoftware=dialog
else
  TUISoftware=dialog
  InstallDependency dialog
fi

if [ ! -x "$(command -v git)" ];then
  InstallDependency git
fi
}

Install(){
URL="https://ipinfo.io"
Address=$(curl -sL ${URL} | sed -n 's/.*"country": "\(.*\)",.*/\1/p')
if [ "${Address}" = "CN" ]
then
  GithubMirror="https://gh.arcticfox.top/"
else
  unset GithubMirror
fi
if ! git clone --depth=1 ${GithubMirror}https://github.com/ArcticFox520/Yunzai-Bot-Script ./AFYZ
then
  echo -e ${red} - ${yellow}出现错误${background}
  exit
fi
cd AFYZ
chmod +x afyz
AF_HOME_Path="$(pwd)"
sed -i "s|AF_HOME_Path|${AF_HOME_Path}|g" afyz
mv -f afyz /usr/local/bin/afyz

echo -e ${yellow} - ${yellow}安装成功${background}
echo -e ${yellow} - ${cyan}请使用 ${green}afyz ${cyan}命令 打开脚本${background}
}

echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${cyan}白狐 Script ${green}是完全可信的。${background}
echo -e ${cyan}白狐 Script ${yellow}不会执行任何恶意命令${background}
echo -e ${cyan}白狐 Script ${yellow}不会执行任何恶意命令${background}
echo -e ${cyan}白狐 Script ${yellow}不会执行任何恶意命令${background}
echo -e ${cyan}如果您同意安装 请输入 ${green}同意安装${background}
echo -e ${cyan}注意：同意安装即同意本项目的用户协议${background}
echo -e ${cyan}用户协议链接: ${background}
echo -e ${cyan}https://raw.githubusercontent.com/ArcticFox520/Yunzai-Bot-Script/main/用户协议.txt${background}
echo -e ${white}"=========================="${background}
echo -en ${green}请输入:${background}
read yn
if [ "${yn}" == "同意安装" ];then
    echo -e ${green}3秒后开始安装${background}
    sleep 2s
    Dependency
    Install
else
    echo -e ${red}程序终止!! 脚本停止运行${background}
fi